/**
 *     tipple is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    tipple is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with tipple.  If not, see <http://www.gnu.org/licenses/>.
 * Copyright 2009 James Osler, Adam Tomkins

 */

package v0_4;
import java.util.*;
import java.text.*;

public class gui extends javax.swing.JFrame {
    
    static List<Beer> beerList = Collections.synchronizedList(new ArrayList<Beer>());
    static List<BeerPC> suggestedList;
    static List<Beer> sortList;
    private boolean repeats;
    NumberFormat df = NumberFormat.getInstance(); // used to format numbers correctly.
    NumberFormat cf = NumberFormat.getCurrencyInstance();
    

    /** Creates new form gui */
    public gui() {
        initComponents();
        df.setMinimumFractionDigits(2);
        df.setMaximumFractionDigits(2);
        cf.setMinimumFractionDigits(2);
        cf.setMaximumFractionDigits(2);
        beerList.clear();
        beerList = readFile.fInput("Beer.txt");
        displayArea.append("Please enter how much money you \nhave to buy beer with in the box above \nand click ok!");
    }

    private void calcHU() {
    //NumberFormat form = NumberFormat.getInstance();
    //form.setMinimumFractionDigits(2);
    //form.setMaximumFractionDigits(2);
    String bName = nameField.getText();
    double bPercent = Double.parseDouble(percentField.getText());
    int bVolume = Integer.parseInt(volumeField.getText());
    double bCost = Double.parseDouble(costField.getText());
    double bHappyUnits = (((bPercent / 100) * bVolume)/bCost);
    happyunitResultLabel.setText(df.format(bHappyUnits));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        moneyLabel = new javax.swing.JLabel();
        goButton = new javax.swing.JButton();
        moneyField = new javax.swing.JTextField();
        displayPanel = new javax.swing.JScrollPane();
        displayArea = new javax.swing.JTextArea();
        repeatsBox = new javax.swing.JCheckBox();
        nameField = new javax.swing.JTextField();
        percentField = new javax.swing.JTextField();
        volumeField = new javax.swing.JTextField();
        nameLabel = new javax.swing.JLabel();
        percentLabel = new javax.swing.JLabel();
        volumeLabel = new javax.swing.JLabel();
        costLabel = new javax.swing.JLabel();
        costField = new javax.swing.JTextField();
        calcTitleLabel = new javax.swing.JLabel();
        happyunitLabel = new javax.swing.JLabel();
        calcButton = new javax.swing.JButton();
        addButton = new javax.swing.JButton();
        bestTitleLabel = new javax.swing.JLabel();
        happyunitResultLabel = new javax.swing.JLabel();
        statusLabel = new javax.swing.JLabel();
        bsortLabel = new javax.swing.JLabel();
        costsortButton = new javax.swing.JButton();
        adcsortButton = new javax.swing.JButton();
        adsortButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Tipple");

        moneyLabel.setText("Money:");

        goButton.setText("Go");
        goButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                goButtonActionPerformed(evt);
            }
        });

        moneyField.setText("0.00");

        displayArea.setColumns(20);
        displayArea.setRows(5);
        displayPanel.setViewportView(displayArea);

        repeatsBox.setText("Repeats");
        repeatsBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                repeatsBoxActionPerformed(evt);
            }
        });

        nameField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nameFieldActionPerformed(evt);
            }
        });

        volumeField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                volumeFieldActionPerformed(evt);
            }
        });

        nameLabel.setText("Name:");

        percentLabel.setText("Percent:");

        volumeLabel.setText("Volume (ml):");

        costLabel.setText("Cost (£):");

        costField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                costFieldActionPerformed(evt);
            }
        });

        calcTitleLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        calcTitleLabel.setText("Calculate happy units for a new beer; then add it to the beer list!");

        happyunitLabel.setText("Happy units:");

        calcButton.setText("Calculate");
        calcButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                calcButtonActionPerformed(evt);
            }
        });

        addButton.setText("Add");
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        bestTitleLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        bestTitleLabel.setText("Enter how much money you have, and we'll recommend the beer!");

        statusLabel.setFont(new java.awt.Font("Trebuchet MS", 2, 13));
        statusLabel.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);

        bsortLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        bsortLabel.setText("Display beers by:");

        costsortButton.setText("Cost");
        costsortButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                costsortButtonActionPerformed(evt);
            }
        });

        adcsortButton.setText("ADC");
        adcsortButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                adcsortButtonActionPerformed(evt);
            }
        });

        adsortButton.setText("AD");
        adsortButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                adsortButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(calcTitleLabel)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(nameLabel)
                            .addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(happyunitLabel)
                                .addGap(18, 18, 18)
                                .addComponent(happyunitResultLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(calcButton, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(addButton, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                                    .addComponent(percentLabel)
                                    .addComponent(percentField, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(volumeLabel)
                                        .addGap(18, 18, 18)
                                        .addComponent(costLabel))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(volumeField, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(costField, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                    .addComponent(statusLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 367, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(moneyLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(moneyField, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(repeatsBox)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(goButton, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(displayPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(29, 29, 29)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(adsortButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(adcsortButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(costsortButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(bsortLabel)))
                    .addComponent(bestTitleLabel))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {addButton, calcButton});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {costField, percentField, volumeField});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(bestTitleLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(moneyLabel)
                    .addComponent(moneyField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(repeatsBox)
                    .addComponent(goButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(displayPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(bsortLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(costsortButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(adcsortButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(adsortButton)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(calcTitleLabel)
                .addGap(14, 14, 14)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(nameLabel)
                        .addGap(26, 26, 26))
                    .addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(volumeLabel)
                            .addComponent(costLabel)
                            .addComponent(percentLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(percentField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(volumeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(costField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(happyunitLabel)
                    .addComponent(calcButton)
                    .addComponent(addButton)
                    .addComponent(happyunitResultLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 28, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void goButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_goButtonActionPerformed
 try {
    double money = Double.parseDouble(moneyField.getText());
    FindBest best = new FindBest(beerList);
    suggestedList = best.suggest(money, repeats);
    displayArea.setText("");
    for (int i=0; i<suggestedList.size(); i++) {
	displayArea.append(suggestedList.get(i).beer.getName() + " - " + cf.format(suggestedList.get(i).beer.getCost()));
	if (suggestedList.get(i).getBuyCount() > 1)
	    displayArea.append("    x" + suggestedList.get(i).getBuyCount() + "\n");
	else 
	    displayArea.append("\n");
	
    }
    displayArea.append("\nWith " + cf.format(best.getRemCash()) + " remaining.");
    statusLabel.setText("");
 }
 catch (Exception e) {
    statusLabel.setText("Please enter your money in the correct format (eg 1.20)");
 }

}//GEN-LAST:event_goButtonActionPerformed

private void repeatsBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_repeatsBoxActionPerformed
   if (repeatsBox.isSelected() == true)
       repeats = true;
   else
       repeats = false;
}//GEN-LAST:event_repeatsBoxActionPerformed

private void nameFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nameFieldActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_nameFieldActionPerformed

private void volumeFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_volumeFieldActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_volumeFieldActionPerformed

private void costFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_costFieldActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_costFieldActionPerformed

private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
    try {
    calcHU();

    IdGen newid = new IdGen();
    int idint = newid.uniqueID(beerList);
    String bName = nameField.getText();
    double bPercent = Double.parseDouble(percentField.getText());
    int bVolume = Integer.parseInt(volumeField.getText());
    double bCost = Double.parseDouble(costField.getText());
    if (bName == null || bName.length() == 0)
	throw new nonameException();
    beerList.add(new Beer(idint, bName, bPercent, bVolume, bCost));
    Filewrite newfile = new Filewrite(idint, bName, bPercent, bVolume, bCost);
    statusLabel.setText(bName + " added to the database.");
    }
     catch (nonameException nne) {
	statusLabel.setText("Please enter a name.");
    }
     catch (Exception e) {
    statusLabel.setText("Please enter beer variables in the correct formats");
     }
}//GEN-LAST:event_addButtonActionPerformed

private void calcButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_calcButtonActionPerformed
    try {
    calcHU();
    statusLabel.setText("");
    }
    catch (Exception e) {
	statusLabel.setText("Please enter beer variables in the correct formats");
    }
}//GEN-LAST:event_calcButtonActionPerformed

private void costsortButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_costsortButtonActionPerformed
     try {
        FindBest cost = new FindBest(beerList);
        sortList = cost.sortCost();
        displayArea.setText("");
        for (int i=0; i<sortList.size(); i++) {
        displayArea.append(sortList.get(i).getName() + " - " + cf.format(sortList.get(i).getCost()));
            displayArea.append("\n");
        }
        statusLabel.setText("");
     }
     catch (Exception e) {
        statusLabel.setText("Error with sortCost.");
     }
}//GEN-LAST:event_costsortButtonActionPerformed

private void adcsortButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_adcsortButtonActionPerformed
     try {
        FindBest ADC = new FindBest(beerList);
        sortList = ADC.sortADC();
        displayArea.setText("");
        for (int i=0; i<sortList.size(); i++) {
        displayArea.append(sortList.get(i).getName() + " - " + df.format(sortList.get(i).getADC()));
            displayArea.append("\n");
        }
        statusLabel.setText("");
     }
     catch (Exception e) {
        statusLabel.setText("Error with sortADC.");
     }
}//GEN-LAST:event_adcsortButtonActionPerformed

private void adsortButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_adsortButtonActionPerformed
     try {
        FindBest AD = new FindBest(beerList);
        sortList = AD.sortAD();
        displayArea.setText("");
        for (int i=0; i<sortList.size(); i++) {
        displayArea.append(sortList.get(i).getName() + " - " + df.format(sortList.get(i).getAD()));
            displayArea.append("\n");
        }
        statusLabel.setText("");
     }
     catch (Exception e) {
        statusLabel.setText("Error with sortAD.");
     }
}//GEN-LAST:event_adsortButtonActionPerformed

    
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new gui().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton adcsortButton;
    private javax.swing.JButton addButton;
    private javax.swing.JButton adsortButton;
    private javax.swing.JLabel bestTitleLabel;
    private javax.swing.JLabel bsortLabel;
    private javax.swing.JButton calcButton;
    private javax.swing.JLabel calcTitleLabel;
    private javax.swing.JTextField costField;
    private javax.swing.JLabel costLabel;
    private javax.swing.JButton costsortButton;
    private javax.swing.JTextArea displayArea;
    private javax.swing.JScrollPane displayPanel;
    private javax.swing.JButton goButton;
    private javax.swing.JLabel happyunitLabel;
    private javax.swing.JLabel happyunitResultLabel;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextField moneyField;
    private javax.swing.JLabel moneyLabel;
    private javax.swing.JTextField nameField;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTextField percentField;
    private javax.swing.JLabel percentLabel;
    private javax.swing.JCheckBox repeatsBox;
    private javax.swing.JLabel statusLabel;
    private javax.swing.JTextField volumeField;
    private javax.swing.JLabel volumeLabel;
    // End of variables declaration//GEN-END:variables

}
